package com.utils
{
	/**
	 * 弹框提示
	 * @author Lufu
	 */		
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.clearInterval;
	import flash.utils.clearTimeout;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	public class Tips extends Sprite
	{
		private static var _stage:Stage;
		private static var _parentContain:DisplayObjectContainer;
		
		private var tipContain:Sprite;
		private var bgContain:Sprite;
		private var txtContain:Sprite;
		private var tipInfoArr:Array = [];
		private var time:int = 50;
		private var txtLen:int;
		private var intervalShow:int;
		private var intervalHide:int;
		private var timeOutClear:int;
		private var tempLength:int;
		
		private static var _tips:Tips;
		public function Tips()
		{
			if(!_stage || !_parentContain){
				throw Error('Tips-->调用register()注册舞台和父容器!');
			}
			
			if(_tips){
				throw Error('Tips不能重复创建实例!')
			}else{
				_tips = this;
			}
			init();
			trace('--->创建Tips实例!');
		}
		
		public static function instance():Tips{
			if(!_tips) _tips = new Tips();
			return _tips;
		}
		
		
		/**
		 * 注册舞台和父容器 
		 * @param stage
		 * @param parentContain
		 */		
		public static function register(root:DisplayObjectContainer,parentContain:DisplayObjectContainer):void
		{
			_stage = root.stage;
			_parentContain = parentContain;
		}
		
		private function init():void{
			_parentContain.addChild(this);
			
			tipContain = new Sprite();
			this.addChild(tipContain);
			
			bgContain = new Sprite();
			tipContain.addChild(bgContain);
			
			txtContain = new Sprite();
			tipContain.addChild(txtContain);
			
			this.mouseEnabled = false;
			this.mouseChildren = false;
			this.visible= false;
		}
		
		
		/**
		 * 添加提示,初始化变量
		 * 提示文本以HtmlText格式显示,可自定义文本样式
		 * 自定义样式显示优先级高于默认设置
		 */	
		
		public function addTip(_tipInfoArr:Array):void{
			tipInfoArr = _tipInfoArr;
			tipInfoArr.push('<font color="#FFFFFF">Automatically closes in 3 seconds!</font>');
			clearTipContain();//清理之前为正常结束的time进度
			
			initVariable();//初始化变量
			
			//getTxtLen();//获取文本长度
			
			addTxt();//添加文本
			
			setContainPoint();//设置容器出事坐标
			
			intervalShow = setInterval(showTip,10);//设置移动执行函数
		}
		
		/**初始化变量*/
		private function initVariable():void{
			tipContain.y = _stage.stageHeight;
			time = 50;
			txtLen = 0;  
		}
		
		///**获取文本长度*/		
		//private function getTxtLen():void{
			//var tempList:Array = tipInfoArr;
			//var font:RegExp =  new RegExp('</font>');
			//var b:RegExp =  new RegExp('</b>');
			//var br:RegExp =  new RegExp('</br>');
			//var i:RegExp =  new RegExp('</i>');
			//for (var j:int = 0; j < tempList.length; j++) 
			//{
				//tempLength= tempList[j].length;
				//if(font.test(tempList[j]) && !b.test(tempList[j])){
					//tempLength = String(tempList[j].split('</font>')[0]).split('>')[1].length;
				//}
				//if(b.test(tempList[j])){
					//tempLength = String(tempList[j].split('</b>')[0]).split('<b>')[1].length;
				//}
				//if(br.test(tempList[j]) || i.test(tempList[j])){
					//tempLength -= 8;
				//}
				//if(tempLength > txtLen) txtLen = tempLength;
			//}
		//}
		
		/**添加文本*/		
		private function addTxt():void{
			
			var txtList:Array = [];
			
			tipInfoArr = RegExpUtil.stringWrap(tipInfoArr);
			
			for (var i:int = 0; i < tipInfoArr.length; i++) 
			{
				var txt:TextField = new TextField();
				txtContain.addChild(txt);
				
				//txt.wordWrap = true;
				txt.autoSize = TextFieldAutoSize.CENTER;
				txt.htmlText = '<font color="#FFFF00" size="15">' + tipInfoArr[i] + '</font>';
				
				txt.width = txt.textWidth;
				txt.y = 22*i + 5;
				txtList.push(txt);
			}
			
			bgContain.graphics.clear();
			bgContain.graphics.lineStyle(1,0xFFFFFF);
			bgContain.graphics.beginFill(0,.8);
			bgContain.graphics.drawRoundRect(0,0,txtContain.width+20,txtContain.height+ 5,10,10);
			bgContain.graphics.endFill();
			for (var j:int = 0; j < txtList.length; j++) 
			{
				txtList[j].x = (bgContain.width - txtList[j].width)/2;
			}
			txtList = null;
		}
		
		/**设置容器坐标*/		
		private function setContainPoint():void{
			tipContain.x = (_stage.stageWidth- tipContain.width)/2;
			tipContain.y = _stage.stageHeight/2 + 50;
		}
		
		
		/**显示*/		
		private function showTip():void{
			tipContain.y -= 10;
			tipContain.alpha += .05;
			if(tipContain.y <= _stage.stageHeight/2 - tipContain.height/2){
				tipContain.alpha = 1;
				clearInterval(intervalShow);
				timeOutClear = setTimeout(function ():void{intervalHide = setInterval(hideTip,10)},3000);
			}
			this.visible= true;
		}
		
		/**隐藏*/		
		private function hideTip():void
		{
			tipContain.y -= 10;
			tipContain.alpha -= .1;
			if(tipContain.alpha < 0){
				clearTipContain();
			}
		}		
		
		/**清理数据*/		
		private function clearTipContain():void{
			clearTimeout(timeOutClear);
			clearInterval(intervalHide);
			clearInterval(intervalShow);
			tipContain.alpha = 0;
			this.visible= false;
			while(txtContain.numChildren > 0) txtContain.removeChildAt(0);
			while(bgContain.numChildren > 0) bgContain.removeChildAt(0);
			//while(tipContain.numChildren > 0) tipContain.removeChildAt(0);
		}
		
	}
}